package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.Stock;
import com.ruoyi.system.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StockLogMapper;
import com.ruoyi.system.domain.StockLog;
import com.ruoyi.system.service.IStockLogService;

/**
 * 库存变更记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-28
 */
@Service
public class StockLogServiceImpl implements IStockLogService 
{
    @Autowired
    private StockLogMapper stockLogMapper;
    @Autowired
    private StockMapper stockMapper;

    /**
     * 查询库存变更记录
     * 
     * @param id 库存变更记录主键
     * @return 库存变更记录
     */
    @Override
    public StockLog selectStockLogById(Long id)
    {
        return stockLogMapper.selectStockLogById(id);
    }

    /**
     * 查询库存变更记录列表
     * 
     * @param stockLog 库存变更记录
     * @return 库存变更记录
     */
    @Override
    public List<StockLog> selectStockLogList(StockLog stockLog)
    {
        return stockLogMapper.selectStockLogList(stockLog);
    }

    /**
     * 新增库存变更记录,变更库存
     * 
     * @param stockLog 库存变更记录
     * @return 结果
     */
    @Override
    public int insertStockLog(StockLog stockLog)
    {
        //查询stock表有没有该商品
        Stock stock_select = new Stock();
        stock_select.setGoodsId(stockLog.getGoodsid());
        stock_select.setShopId(stockLog.getShopId());
        List<Stock> list = stockMapper.selectStockList(stock_select);
        //变更库存
        //如果有，则更新库存
        if(list!=null && list.size()>0){
            Stock stock1 = list.get(0);
            System.out.println(stock1.toString());
            //判断变更后库存是否为负数
            if(stock1.getStock() + stockLog.getGoodsNum() < 0){
                return 0;
            }
            double oldStock = stock1.getStock();
            stockLog.setBeforeStock(oldStock);
            stockLog.setAfterStock(oldStock + stockLog.getGoodsNum());
            stock1.setStock(stock1.getStock() + stockLog.getGoodsNum());
            stockMapper.updateStock(stock1);
        }else{//没有则新增对应商品库存
            //判断库存是否为负数
            if(stockLog.getGoodsNum() < 0){
                return 0;
            }
            stock_select.setShopName(stockLog.getShopName());
            stock_select.setStock(stockLog.getGoodsNum());
            stock_select.setGoodsName(stockLog.getGoodsName());
            stock_select.setCompanyId(stockLog.getCompanyId());
            stock_select.setRemark(stockLog.getRemark());
            stockLog.setBeforeStock(0.0);
            stockLog.setAfterStock(stockLog.getGoodsNum());
            stockMapper.insertStock(stock_select);
        }

        //新增记录
        int result = stockLogMapper.insertStockLog(stockLog);

        return result;
    }

    /**
     * 修改库存变更记录
     * 
     * @param stockLog 库存变更记录
     * @return 结果
     */
    @Override
    public int updateStockLog(StockLog stockLog)
    {
        return stockLogMapper.updateStockLog(stockLog);
    }

    /**
     * 批量删除库存变更记录
     * 
     * @param ids 需要删除的库存变更记录主键
     * @return 结果
     */
    @Override
    public int deleteStockLogByIds(Long[] ids)
    {
        return stockLogMapper.deleteStockLogByIds(ids);
    }

    /**
     * 删除库存变更记录信息
     * 
     * @param id 库存变更记录主键
     * @return 结果
     */
    @Override
    public int deleteStockLogById(Long id)
    {
        return stockLogMapper.deleteStockLogById(id);
    }
}
